package cn.maidouya.order.service.impl;

import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.basic.util.CodeGenerateUtils;
import cn.maidouya.basic.util.DistanceUtil;
import cn.maidouya.basic.util.Point;
import cn.maidouya.order.domain.OrderAddress;
import cn.maidouya.order.domain.ProductOrder;
import cn.maidouya.order.domain.ProductOrderDetail;
import cn.maidouya.order.mapper.OrderAddressMapper;
import cn.maidouya.order.mapper.ProductOrderDetailMapper;
import cn.maidouya.order.mapper.ProductOrderMapper;
import cn.maidouya.order.service.IProductOrderService;
import cn.maidouya.org.domain.Shop;
import cn.maidouya.org.mapper.ShopMapper;
import cn.maidouya.pay.constants.OrderConstants;
import cn.maidouya.pay.domain.PayBill;
import cn.maidouya.pay.mapper.PayBillMapper;
import cn.maidouya.pay.service.IPayBillService;
import cn.maidouya.product.domain.Product;
import cn.maidouya.product.domain.ProductDetail;
import cn.maidouya.product.mapper.ProductMapper;
import cn.maidouya.user.domain.Logininfo;
import cn.maidouya.user.domain.User;
import cn.maidouya.user.domain.UserAddress;
import cn.maidouya.user.mapper.UserAddressMapper;
import cn.maidouya.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService payBillService;
    @Override
    public void cancelOrder(Long orderId) {

    }

    @Override
    public String submit(Map<String, Object> params, Logininfo logininfo) {

        Integer productNum = Integer.valueOf(params.get("product_num").toString()); // 销售量
        Long productId = Long.valueOf(params.get("product_id").toString()); // 商品id
        Long addressId = Long.valueOf(params.get("address_id").toString()); // 地址
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString()); // 服务方式
        Long payMethod = Long.valueOf(params.get("pay_method").toString()); // 支付方式
        System.err.println(productId + logininfo.getId());
        // 查询
        Product product = productMapper.loadById(productId); // 查询商品
        User user = userMapper.loadByLogininfoId(logininfo.getId()); // 查询当前登录人信息
        UserAddress userAddress = userAddressMapper.loadById(addressId); // 查询用户选择地址

        // 1. 生成订单

        ProductOrder productOrder = new ProductOrder();
        String unionPaySn = CodeGenerateUtils.generateUnionPaySn();
        productOrder.setPaySn(unionPaySn);
            productOrder.setDigest("摘要："+product.getName()+"的服务订单");// 买哪些商品
            productOrder.setPrice(product.getSaleprice().multiply(new BigDecimal(productNum))); // 商品总价格
            productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
            productOrder.setLastConfirmTime(new Date(System.currentTimeMillis()+OrderConstants.ORDER_TIMEOUT));
            productOrder.setProduct_id(productId);
            productOrder.setUser_id(user.getId());
            // 分配最近店铺
            List<Shop> shops = shopMapper.loadAll(); // 查询所有店铺
            Point point = DistanceUtil.getPoint(userAddress.getFullAddress());
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            productOrder.setShop_id(nearestShop.getId());
        productOrderMapper.save(productOrder);

        // 2. 订单详细(下单后保留证据)
        ProductOrderDetail detail = new ProductOrderDetail();
            BeanUtils.copyProperties(product,detail);
            detail.setId(null);
            detail.setProduct_id(productId);
            detail.setOrder_id(productOrder.getId());
        productOrderDetailMapper.save(detail);

        // 3. 订单地址
        OrderAddress orderAddress = new OrderAddress();
            BeanUtils.copyProperties(userAddress,orderAddress);
            orderAddress.setId(null);
            orderAddress.setOrder_id(orderAddress.getId());
            orderAddress.setOrderSn(orderAddress.getOrderSn());
        orderAddressMapper.save(orderAddress);

        //  4.服务商品销量
        product.setSalecount(product.getSalecount() + productNum);
        productMapper.update(product);


        // 5. 生成支付单
        PayBill bill = new PayBill();
                bill.setDigest(productOrder.getDigest());  // 订单摘要
                bill.setMoney(productOrder.getPrice());  // 订单价格
                bill.setUnionPaySn(productOrder.getPaySn());  // 统一支付单
                bill.setLastPayTime(new Date(System.currentTimeMillis() + OrderConstants.ORDER_TIMEOUT));  // 最后确认时间
                bill.setPayChannel(payMethod);  // 支付方式
                bill.setBusinessType(OrderConstants.BUSINESS_TYPE_PRODUCT_ORDER); // 订单类型
                bill.setBusinessKey(productOrder.getId()); // 订单业务id
                bill.setUser_id(user.getId());  // 领养用户商品
                bill.setShop_id(productOrder.getShop_id());  // 店铺id
                bill.setNickName(user.getUsername());  // 用户昵称
        payBillMapper.save(bill);

            // 6 调用统一支付接口
        return payBillService.pay(bill);
        // 7. @TODO 订单定时取消器
    }
}